package insight.google.game.era.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.proxy.ScriptProxy;

import insight.google.game.era.bo.GameUser;
import insight.google.game.era.client.proxy.PlayerClientProxyImpl;
import insight.google.game.era.core.GameCenterManager;
import insight.google.game.era.core.IGameCenter;
import insight.google.game.era.core.Util;
import insight.google.game.era.data.TransactionHandler;
import insight.google.game.era.data.User;
import insight.google.game.era.data.UserDataManager;

@RemoteProxy(name = "HostServiceCenter")
public class HostServiceCenterImpl implements HostServiceCenter {
	// @Override
	// public void welcome(String redirectUrl) {
	// // TODO Auto-generated method stub
	// GameCenter gameCenter = GameCenter.getInstance();
	// gameCenter.welcome(redirectUrl);
	// }

	private static final Log log = LogFactory
			.getLog(HostServiceCenterImpl.class);

	@RemoteMethod
	public String login(String userName) {
		GameCenterManager gameCenter = GameCenterManager.getInstance();
		// gameCenter.login(userName);
		return userName;
	}

	@RemoteMethod
	public void initMainPage(String jsComponentId) {
		GameCenterManager gameCenter = GameCenterManager.getInstance();
		gameCenter.initShowMainPage(jsComponentId);
	}

	@RemoteMethod
	public void join(String roomName, String jsComponentId, int dataVersion) {
		GameCenterManager gameCenter = GameCenterManager.getInstance();
		if (!gameCenter.checkDataVersion(dataVersion)) {
			try {
				gameCenter.join(roomName, jsComponentId);
			} catch (Exception e) {
				log.error(e.getLocalizedMessage());
				e.printStackTrace();
				return;
			}
		}
	}

	@RemoteMethod
	public void logout(String jsComponentId) {
		GameCenterManager gameCenter = GameCenterManager.getInstance();
		gameCenter.logout(jsComponentId);
	}

	@RemoteMethod
	public void onLoad() throws Exception{
		log.debug(" HostServiceCenterImpl onload() --  " + this.toString());
		GameCenterManager.getInstance().onLoad();
//		try {
//		} catch (Exception e) {
//			e.printStackTrace();
//			log.error(e.getLocalizedMessage());
//			return;
//		}
	}

	@RemoteMethod
	public void reverseAjaxAcknowledgement(String data) {
		// TODO Auto-generated method stub
		// GameCenter gameCenter = GameCenter.getInstance();
		// GameUser user = gameCenter.getUserBySession();
		// user.clearReverseCall();

	}

	@RemoteMethod
	@Override
	public void changeUserProfile(String nickName) {
		UserDataManager userDataManager = null;
		try {
			userDataManager = new UserDataManager(Util
					.createTranscationHandler());
			User user = Util.getUserBySession(userDataManager
					.getTransactionHandler());
			user.setName(nickName);
			userDataManager.addToDataStore(user);
			user.setScriptProxy(new ScriptProxy(Util.getUserScriptSession(user.getName())));
			userDataManager.commit();
			PlayerClientProxyImpl.getInstance().notifyOtherUserForUpdatedProfile(user.getScriptProxy(), "room_instance", user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (userDataManager != null) {
				userDataManager.close();
			}
		}

	}

	@RemoteMethod
	@Override
	public void clearCache() {
		GameCenterManager gameCenter = GameCenterManager.getInstance();
		gameCenter.clearDataStore();
	}

}
